require "loginAwardCfg"

LoginAwardPanel = class("LoginAwardPanel")


local img_tts = {
    "loginAward/1d.png",
    "loginAward/2d.png",
    "loginAward/3d.png",
    "loginAward/4d.png",
    "loginAward/5d.png",
    "loginAward/6d.png",
    "loginAward/7d.png",
}
local img_tts_a = {
    "loginAward/1d - a.png",
    "loginAward/2d - a.png",
    "loginAward/3d - a.png",
    "loginAward/4d - a.png",
    "loginAward/5d - a.png",
    "loginAward/6d - a.png",
    "loginAward/7d - a.png",
}

local img_bd_hightlight = "loginAward/fag.png"
local img_bd_a = "loginAward/tiaohui.png"

local img_got = "loginAward/gou.png"
local img_get = "loginAward/lingqu.png"
local img_get_bg = "loginAward/lqd.png"

local img_coin = "bubbles/coin.png"
local img_fenji = "bubbles/fenji.png"
local img_yuheng = "bubbles/yuheng.png"


local s_uiinfo = s_uiinfo or {}
s_uiinfo.tt = UIInfo.create()
s_uiinfo.img0 = UIInfo.create()
s_uiinfo.fnt0 = UIInfo.create()
s_uiinfo.img1 = UIInfo.create()
s_uiinfo.fnt1 = UIInfo.create()
s_uiinfo.img2 = UIInfo.create()
s_uiinfo.fnt2 = UIInfo.create()

s_uiinfo.getbg = UIInfo.create()
s_uiinfo.got = UIInfo.create()
s_uiinfo.get = UIInfo.create()



function LoginAwardPanel.create(parent, awardStatus)
    local panel = LoginAwardPanel.new()
    panel.awardStatus = awardStatus
    panel:init(parent)
    return panel
end


function LoginAwardPanel:init(parent)
    self.view = ccs.GUIReader:getInstance():widgetFromJsonFile("loginAwardPanel.json")
    parent.widget:addChild(self.view)
    self.view:setLocalZOrder(pp.z_loginAward)
    self.parent = parent

  
    self.bds = {}

    
    for i = 1,7 do

        local bdname = string.format("bd%d", i)
        local bd = self.view:getChildByName(bdname)
        self.bds[#self.bds+1] = bd

        if i == 1 then
            local tt = bd:getChildByName("_tt")
            s_uiinfo.tt:readInfoFromNode(tt)
            tt:removeFromParent(true)

            local img0 = bd:getChildByName("_image0")
            s_uiinfo.img0:readInfoFromNode(img0)
            img0:removeFromParent(true)

            local fnt0 = bd:getChildByName("_fnt0")
            s_uiinfo.fnt0:readInfoFromNode(fnt0)
            fnt0:removeFromParent(true)

            local getbg = bd:getChildByName("_get_bg")
            s_uiinfo.getbg:readInfoFromNode(getbg)
            getbg:removeFromParent(true)

            local get = bd:getChildByName("_get")
            s_uiinfo.get:readInfoFromNode(get)
            get:removeFromParent(true)

            local got = bd:getChildByName("_got")
            s_uiinfo.got:readInfoFromNode(got)
            got:removeFromParent(true)

        elseif i == 2 then
            local img1 = bd:getChildByName("_image1")
            s_uiinfo.img1:readInfoFromNode(img1)
            img1:removeFromParent(true)

            local fnt1 = bd:getChildByName("_fnt1")
            s_uiinfo.fnt1:readInfoFromNode(fnt1)
            fnt1:removeFromParent(true)

            local img2 = bd:getChildByName("_image2")
            s_uiinfo.img2:readInfoFromNode(img2)
            img2:removeFromParent(true)

            local fnt2 = bd:getChildByName("_fnt2")
            s_uiinfo.fnt2:readInfoFromNode(fnt2)
            fnt2:removeFromParent(true)
        end

        local function btCallback(sender, eventType)
            if not self.bAlreadyGetAward and eventType == ccui.TouchEventType.began then
                self:gettingAward(i)
                self:refreshGetToGot()
                self:popOut(0)
                
                self.parent.loginAwardPanel = nil
                self.parent:refreshCoin(userData.coin)

                AudioEngine.playEffect(sound.btn1)
                self.bAlreadyGetAward = true
                uiIndicator:remove()

                local function checkPopLB()
                    self.parent:checkPopLiBao()
                end
                performWithDelay(self.parent.view, checkPopLB, 0.7)
            end
        end
        bd:addTouchEventListener(btCallback)
    end

    --del
    -- local function getImageBygameObjRef(otype)
    --     local retimage
    --     if otype == pp.gameObjs.coin then
    --         retimage = img_coin
    --     elseif otype == pp.gameObjs.fenji then
    --         retimage = img_fenji
    --     elseif otype == pp.gameObjs.yuheng then
    --         retimage = img_yuheng
    --     end
    --     return retimage 
    -- end

    local fntname = pp.fnt_number_yellow

    --place award
    for i = 1, 7 do
        local award = loginAwardCfg[i]
        if #award == 1 then
            do
                local img_ = ccui.ImageView:create(award[1].gameObjRef.img, ccui.TextureResType.localType)
                s_uiinfo.img0:setInfoToNode(img_)
                self.bds[i]:addChild(img_)
 
                local fnt_ = cc.LabelBMFont:create(string.format("%d",award[1].cnt), fntname)
                s_uiinfo.fnt0:setInfoToNode(fnt_)
                self.bds[i]:addChild(fnt_)
                fnt_:setScale(0.7)
            end

        elseif #award == 2 then
            do
                local img_ = ccui.ImageView:create(award[1].gameObjRef.img, ccui.TextureResType.localType)
                s_uiinfo.img1:setInfoToNode(img_)
                self.bds[i]:addChild(img_)

                local fnt_ = cc.LabelBMFont:create(string.format("%d",award[1].cnt), fntname)
                s_uiinfo.fnt1:setInfoToNode(fnt_)
                self.bds[i]:addChild(fnt_)
                fnt_:setScale(0.7)
            end
            do
                local img_ = ccui.ImageView:create(award[2].gameObjRef.img, ccui.TextureResType.localType)
                s_uiinfo.img2:setInfoToNode(img_)
                self.bds[i]:addChild(img_)

                local fnt_ = cc.LabelBMFont:create(string.format("%d",award[2].cnt), fntname)
                s_uiinfo.fnt2:setInfoToNode(fnt_)
                self.bds[i]:addChild(fnt_)
                fnt_:setScale(0.7)             
            end

        end

    end

    --draw by awardinfo
    local continueTime = userData.continuesLoginAwardTimes
    --lkj temp: test
    -- continueTime = 4

    self.getbdIdx = continueTime + 1
    self.getBdGetIcon = nil

    for i = 1,7 do
        if i <= self.getbdIdx then
            local tt = ccui.ImageView:create(img_tts[i], ccui.TextureResType.localType)
            s_uiinfo.tt:setInfoToNode(tt)
            self.bds[i]:addChild(tt)

            local getbg = ccui.ImageView:create(img_get_bg, ccui.TextureResType.localType)
            s_uiinfo.getbg:setInfoToNode(getbg)
            self.bds[i]:addChild(getbg)


            if i == self.getbdIdx then
                self.bds[i]:setEnabled(true)
                self.bds[i]:loadTextureNormal(img_bd_hightlight, ccui.TextureResType.localType)

                self.getBdGetIcon = ccui.ImageView:create(img_get, ccui.TextureResType.localType)
                s_uiinfo.get:setInfoToNode(self.getBdGetIcon)
                self.bds[i]:addChild(self.getBdGetIcon)

                
                local function showIndicate()
                    local ind = uiIndicator:create(true)
                    parent.widget:addChild(ind, pp.z_loginAward+1)
                    ind:setPosition(self.bds[i]:getPositionX() + 380, self.bds[i]:getPositionY() -30 + pp.visibleSize.height)
                    
                end
                performWithDelay(parent.widget,showIndicate,0.8)
            else
                self.bds[i]:setEnabled(false)
               
                local got = ccui.ImageView:create(img_got, ccui.TextureResType.localType)
                s_uiinfo.got:setInfoToNode(got)
                self.bds[i]:addChild(got)
            end

        else
            self.bds[i]:setEnabled(false)
            self.bds[i]:loadTextureNormal(img_bd_a, ccui.TextureResType.localType)

            local tt = ccui.ImageView:create(img_tts_a[i], ccui.TextureResType.localType)
            s_uiinfo.tt:setInfoToNode(tt)
            self.bds[i]:addChild(tt)
        end

    end

end

function LoginAwardPanel:refreshGetToGot()
    if self.getBdGetIcon then
        self.getBdGetIcon:loadTexture(img_got, ccui.TextureResType.localType)
    end
end

function LoginAwardPanel:gettingAward(dayIdx)
    local award = loginAwardCfg[dayIdx]
    gameUtil.gettingLoginAward(award)
end

function LoginAwardPanel:popIn(delay)
    local function inAction()
            local ui = self.view
            uiEffect.MoveIn(ui, 
                cc.p(pp.center.x, pp.center.y*2 + 200),
                cc.p(pp.center.x, pp.center.y*2 + 20)
                )
        end
    self.view:runAction(cc.Sequence:create(
        cc.DelayTime:create(delay),
        cc.CallFunc:create(inAction)
        ))
--    self.parent.mask:setVisible(true)
    self.parent:base_onOpenWin(self)
end 

function LoginAwardPanel:popOut(delay)
    local function inAction()
            local ui = self.view
            uiEffect.MoveOut(ui, 
                cc.p(pp.center.x, pp.center.y*2 + 20),
                cc.p(pp.center.x, pp.center.y*2 + 1200)
                )
        end
    self.view:runAction(cc.Sequence:create(
        cc.DelayTime:create(delay),
        cc.CallFunc:create(inAction)
        ))
    self.parent:base_onCloseWin(self)
end












